package pentago.engine;

import java.awt.Point;

import org.newdawn.slick.Image;
import org.newdawn.slick.Input;

public class BoardPlate extends ClickableObject {

    private byte platePosition;

    public BoardPlate(GameEngine gameEngine, int xPos, int yPos, Image img,
	    float scale, byte platePosition) {
	super(gameEngine, xPos, yPos, img);
	this.width *= scale;
	this.height *= scale;
	this.platePosition = platePosition;
    }

    @Override
    public void executeClick(int xPos, int yPos, int button) {
	if (button != Input.MOUSE_LEFT_BUTTON)
	    return;

	if (!isClicked(xPos, yPos))
	    return;

	int x = -1;
	int y = -1;
	int shiftRight = this.xPos + GameBoard.LEFT_PLATE_SPACING;
	int shiftDown = this.yPos + GameBoard.TOP_PLATE_SPACING;
	for (int i = 0; i < 3; i++) {
	    if (xPos >= shiftRight
		    && xPos <= shiftRight + GameBoard.MARBLE_RIGHT_PIXEL_SHIFT)
		x = i;
	    if (yPos >= shiftDown
		    && yPos <= shiftDown + GameBoard.MARBLE_DOWN_PIXEL_SHIFT)
		y = i;
	    shiftRight += GameBoard.MARBLE_RIGHT_PIXEL_SHIFT;
	    shiftDown += GameBoard.MARBLE_DOWN_PIXEL_SHIFT;
	}
	if (x == -1 || y == -1)
	    return;

	gameEngine.plateClicked(platePosition, new Point(x, y));
    }

    public byte getPlatePosition() {
	return platePosition;
    }

}
